﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OptimumInfra.ViewData;
using OptimumInfra.Models;
using System.Collections.Specialized;

namespace OptimumInfra.Controllers
{
    public class ServiceSupportController : Controller
    {
        //
        // GET: /ServiceSupport/
        private ServiceRepository repo = new ServiceRepository();
        public ActionResult Index()
        {

            string message = Request.QueryString["step"];
            if (message != null || message == "")
                ViewData["message"] = "Step 3/3: Choose your support services";
            ViewData["services"] = this.repo.GetAllServices();
            return View(ViewData);
        }

        //
        // GET: /ServiceSupport/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ServiceSupport/Create
        [Authorize(Roles = "admin")]
        public ActionResult Create()
        {

            SupportService service = new SupportService();
            return View(service);
        } 

        //
        // POST: /ServiceSupport/Create

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                SupportService service = new SupportService();
                UpdateModel(service);
                service = this.repo.CreateService(service);

                TempData["info"] = "Create service successfully";
                return RedirectToAction("Edit", "ServiceSupport", new { id = service.Id });
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ServiceSupport/Edit/5
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            ViewDataSupportServices service = this.repo.GetViewDataServiceById(id);
            return View(service);
        }

        //
        // POST: /ServiceSupport/Edit/5

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                ViewDataSupportServices service = new ViewDataSupportServices();
                UpdateModel(service);
                SupportService r = new SupportService();
                r.Id = service.Id;
                r.Item = service.Name;
                r.Description = service.Description;
                r.Unit = service.Unit;

                this.repo.UpdateService(r);
                TempData["info"] = "Update service successfully";
                return RedirectToAction("Edit", "ServiceSupport", new { id = id });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ServiceSupport/Delete/5
        [Authorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {
            SupportService resource = this.repo.GetServiceById(id);
            this.repo.DeleteServiceById(resource);
            return RedirectToAction("Manage");
        }
        [Authorize(Roles = "admin")]
        public ActionResult Manage()
        {
            ViewData["services"] = this.repo.GetAllServices();
            return View(ViewData);
        }

        [Authorize(Roles = "admin")]
        public ActionResult DeleteAttribute(int id)
        {
            string a = Request.QueryString["amount"];
            int amount = 0;
            try
            {
                amount = Int32.Parse(a);
            }
            catch (Exception e)
            {
                return RedirectToAction("Edit", "servicesupport", new { id = id });
            }
            if (amount == 0)
                return RedirectToAction("Edit", "servicesupport", new { id = id });
            OptimumInfra.Models.Attribute attribute = this.repo.GetAttributeByAmount(amount, id);
            this.repo.DeleteAttributeById(attribute);
            TempData["info"] = "Delete attribute successfully";
            return RedirectToAction("Edit", "ServiceSupport", new { id = id });
        }
        
        //For tst
        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult SavePrice(int id)
        {

            NameValueCollection nvc = Request.Form;
            int amount = 0, price = 0;
            if (!string.IsNullOrEmpty(nvc["new_amount"]))
            {
                amount = Int32.Parse(nvc["new_amount"]);
            }

            if (!string.IsNullOrEmpty(nvc["new_price"]))
            {
                price = Int32.Parse(nvc["new_price"]);
            }
            if (amount != 0 && price != 0)
            {
                this.repo.CreateServiceAttribute(id, amount, price);
            }
            TempData["info"] = "Create new value successfully";
            return RedirectToAction("Edit", "ServiceSupport", new { id = id });

        }
    }
}
